/**
 * @Title: BlogInfoController.java
 * @Package com.zll.ccp.controller.blog
 * @author zhanglinlu
 * @date 2019年5月10日 下午2:22:20
 */
package com.zll.ccp.controller.blog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.zll.ccp.annotation.RequireOwn;
import com.zll.ccp.model.pojo.ResourceType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zll.ccp.model.blog.BlogComment;
import com.zll.ccp.model.blog.BlogReply;
import com.zll.ccp.service.blog.IBlogService;
import com.zll.ccp.utils.CommonUtils;

/**
 * @ClassName: BlogInfoController
 * @Description: 博客详情
 * @author zhanglinlu
 * @date 2019年5月10日 下午2:22:20
 * 
 */
@Controller
public class BlogInfoController {
    @Autowired
    private IBlogService blogService;

    /**
     * @Title: blogInfo
     * @Description: 进入博客详情页面
     * @return
     */
    @RequestMapping("/bloginfo")
    public String blogInfo(String id, HttpServletRequest request) {
        request.setAttribute("id", id);
        return "blog/blogInfo";
    }

    /**
     * @Title: getInitBlog
     * @Description: 获取初始化博客数据
     * @param id 主键id
     * @return 初始化博客数据
     */
    @GetMapping("/initblog")
    @ResponseBody
    public Object getInitBlog(String id) {
        return blogService.getBlogDTOById(id);
    }

    /**
     * @Title: addComment
     * @Description: 增加博客评论
     * @param content 评论内容
     * @return
     */
    @GetMapping("/addcomment")
    @ResponseBody
    public Object addComment(String content, String blogId, HttpSession session) {
        BlogComment blogComment = new BlogComment();
        blogComment.setId(CommonUtils.getUUID());
        blogComment.setUserId(session.getAttribute("userId").toString());
        Date nowDate = CommonUtils.getNowDate();
        blogComment.setCreateTime(nowDate);
        blogComment.setContent(content);
        blogComment.setBlogId(blogId);
        blogService.addBlogComment(blogComment);
        JSONObject result = JSON.parseObject(JSON.toJSONString(blogComment));
        result.put("username", session.getAttribute("userName").toString());
        result.put("createTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(nowDate));
        result.put("replys", new ArrayList());
        return result;
    }

    /**
     * @Title: getInitBlogComment
     * @Description: 获取初始化评论数组
     * @param blogId 博客id
     * @return 评论数组
     */
    @GetMapping("/initcomment")
    @ResponseBody
    public Object getInitBlogComment(String blogId) {
        return blogService.getBlogCommentsById(blogId);
    }

    /**
     * @Title: addBlogReply
     * @Description: 增加回复
     * @param commentId 被回复的评论
     * @param content 回复内容
     * @param toUserId 被回复的user
     * @param session
     * @return
     */
    @GetMapping("/addreply")
    @ResponseBody
    public Object addBlogReply(String commentId, String content, String toUserId, HttpSession session) {
        BlogReply blogReply = new BlogReply();
        blogReply.setCommentId(commentId);
        blogReply.setContent(content);
        blogReply.setCreateTime(CommonUtils.getNowDate());
        blogReply.setFromUserId(session.getAttribute("userId").toString());
        blogReply.setId(CommonUtils.getUUID());
        blogReply.setToUserId(toUserId);
        blogService.addBlogReply(blogReply);
        return null;
    }

    /**
     * @Title: deleteBlogCommentById
     * @Description: 根据id删除
     * @param id
     * @return
     */
    @RequireOwn(resourceType = ResourceType.comment,paramName = "id")
    @GetMapping("/deletecomment")
    @ResponseBody
    public Object deleteBlogCommentById(String id, String objname, HttpSession session) {
        if ((boolean)session.getAttribute("admin")
            || StringUtils.equals(objname, session.getAttribute("userName").toString())) {
            blogService.deleteCommentById(id);
        }
        return null;
    }

    /**
     * @Title: deleteBlogReplyById
     * @Description: 根据id删除回复
     * @param id
     * @return
     */
    @RequireOwn(resourceType = ResourceType.reply,paramName = "id")
    @GetMapping("/deletereply")
    @ResponseBody
    public Object deleteBlogReplyById(String id, String objname, HttpSession session) {
        if ((boolean)session.getAttribute("admin")
            || StringUtils.equals(objname, session.getAttribute("userName").toString())) {
            blogService.deleteReplyById(id);
        }
        return null;
    }

}
